package kr.hkit.androidcomplete02.ch19;

import kr.hkit.androidcomplete02.R;
import kr.hkit.androidcomplete02.ch19.HandlerEx.BackThread;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

public class HandlerPost extends Activity {
	int mainValue;
	int backValue;
	TextView mainTv;
	TextView backTv;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch19_threadex);
		mainTv = (TextView) findViewById(R.id.ch19_thread_maintv);
		backTv = (TextView) findViewById(R.id.ch19_thread_backtv);
		
		BackThread thread = new BackThread();
		thread.setDaemon(true);
		thread.start();
	}
	
	public void mOnClick(View v){
		if(v.getId() == R.id.ch19_thread_btn){
			mainValue++;
			mainTv.setText("MainValue : " + mainValue);

		}
	}
	
	class BackThread extends Thread{
		@Override
		public void run() {
			while(true){
				backValue++;
				mHandler.post(new Runnable() {					
					@Override
					public void run() {
						backTv.setText("BackValue : " + backValue);						
					}
				});
				try{
					Thread.sleep(1000);
				}catch(InterruptedException e){
				}
			}
		}
		
		Handler mHandler = new Handler();
	}
}
